package com.ts.controller.achievement;

import com.ts.constant.AchievementConstant;
import com.ts.constant.AchievementStatusConstant;
import com.ts.dto.MonographDTO;
import com.ts.result.Result;
import com.ts.service.achievement.MonographService;
import com.ts.service.achievement.TsAchievementService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 专著成果相关接口 - 专著成果的提交
 */
@RestController
@RequestMapping("/achievement/monograph")
@Tag(name = "专著成果相关接口", description = "专著成果的提交功能")
@Slf4j
public class MonographController {

    @Autowired
    private MonographService monographService;
    @Autowired
    private TsAchievementService tsAchievementService;

    /**
     * 提交一个专著成果
     * @param monographDTO 专著成果数据传输对象
     * @return 操作结果
     */
    @Operation(summary = "提交", description = "提交专著成果")
    @PostMapping()
    public Result saveMonograph(@RequestBody MonographDTO monographDTO) {
        log.info("接收到专著成果提交请求，数据：{}", monographDTO);
        try {
            monographService.save(monographDTO);
            String type = AchievementConstant.MONOGRAPH;
            String achievename = monographDTO.getName();
            String descriptipn = "1111";
            Integer level = 2;
            Integer  createdBy = 1;
            Long softId = monographDTO.getId();
            tsAchievementService.addAchievement(achievename
                    ,descriptipn
                    ,type
                    ,level
                    ,createdBy
                    ,softId);
            log.info("专著成果提交成功");
            return Result.success();
        } catch (Exception e) {
            log.error("专著成果提交失败", e);
            return Result.error("专著成果提交失败");
        }
    }
    @Operation(summary = "更新", description = "更新专著成果")
    @PutMapping()
    public Result updateMonograph(@RequestBody MonographDTO monographDTO) {
        log.info("接收到专著成果更新请求，数据：{}", monographDTO);
        try {
            monographService.update(monographDTO);
            log.info("专著成果更新成功");
            return Result.success();
        } catch (Exception e) {
            log.error("专著成果更新失败", e);
            return Result.error("专著成果更新失败");
        }
    }

    @Operation(summary = "删除", description = "删除专著成果")
    @DeleteMapping("/{id}")
    public Result deleteMonograph(@PathVariable Long id) {
        log.info("接收到专著成果删除请求，ID：{}", id);
        try {
            monographService.delete(id);
            log.info("专著成果删除成功");
            return Result.success();
        } catch (Exception e) {
            log.error("专著成果删除失败", e);
            return Result.error("专著成果删除失败");
        }
    }

    @Operation(summary = "查询单个", description = "根据ID查询专著成果")
    @GetMapping("/{id}")
    public Result<MonographDTO> getMonographById(@PathVariable Long id) {
        log.info("接收到专著成果查询请求，ID：{}", id);
        try {
            MonographDTO dto = monographService.getById(id);
            log.info("专著成果查询成功");
            return Result.success(dto);
        } catch (Exception e) {
            log.error("专著成果查询失败", e);
            return Result.error("专著成果查询失败");
        }
    }

    @Operation(summary = "查询所有", description = "查询所有专著成果")
    @GetMapping()
    public Result<List<MonographDTO>> listAllMonographs() {
        log.info("接收到所有专著成果查询请求");
        try {
            List<MonographDTO> list = monographService.listAll();
            log.info("专著成果列表查询成功");
            return Result.success(list);
        } catch (Exception e) {
            log.error("专著成果列表查询失败", e);
            return Result.error("专著成果列表查询失败");
        }
    }
}
